package com.spider.im.common.aspect;

import com.spider.im.common.constant.RespCodeEnum;
import io.netty.util.internal.ThrowableUtil;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

/**
 * Controller层统一异常处理
 */
@Slf4j
@Aspect
@Component
public class GlobalExceptionHandlerAspect {

    @Around("execution(* com.spider.im..*Controller.*(..))")
    public Object handleException(ProceedingJoinPoint joinPoint) throws Throwable {
        try {
            return joinPoint.proceed();
        } catch (BusinessException e) {
            log.warn("处理请求失败: {},{}", e.getMessage(), ThrowableUtil.stackTraceToString(e));
            return RespResult.error(e.getErrorCode(),  e.getMessage());
        } catch (Exception e) {
            log.error("处理请求失败: {}", ThrowableUtil.stackTraceToString(e));
            return RespResult.error(RespCodeEnum.FAIL.getCode(), RespCodeEnum.FAIL.getDesc());
        }
    }
}